package vtc;

import java.util.*;

public class FSM extends FunctionalUnit
{
	VtcFSM fsm_;
	
	FSM(String id, VtcArrayBlockingQueue inQueues[], VtcArrayBlockingQueue outQueues[], VtcFSM fsm)
	{
		super(id, inQueues, outQueues);
		fsm_ = fsm;
	}
	
	public void functionalProcess()
	{
		Object [] eventv;
		try
		{
			eventv = (Object [])inValue_.getEvent();
		}
		catch(Exception e)
		{
			System.out.print("Input event in FSM::functionalProcess is not an Object[].");
			return;
		}
		Date timeTag = inValue_.getTimeTag();
		String transition = (String)eventv[0];
		try
		{
			if(fsm_.existTransition(fsm_.getCurrentState(), transition))
			{
				fsm_.apply(transition);
				Object [] newEventv = new Object[eventv.length-1];
				for(int i = 1; i < eventv.length ; i++)
				{
					newEventv[i-1] = eventv[i];
				}
				outValue_ = new VtcTimeTaggedEvent(newEventv, timeTag);
				System.out.print("New state: " + fsm_.getCurrentState() + "\n");
			}
		}
		catch(VtcFSMException e)
		{
			System.out.print(e.getMessage());
		}
	}
}